package com.lhd.common.utils;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DateFormatUtil {
    public static void main(String[] args) {
//        String  str="2012-10-02 15:03:22";
//        LocalDateTime p = LocalDateTime.parse(str, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//        System.out.println(p);
//        long l = p.toInstant(ZoneOffset.of("+1")).toEpochMilli();
//        System.out.println(l);
             String  s="2012-01-02 11:22:33";
        System.out.println(toTs(s,true));

    }

    private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private static final DateTimeFormatter dtfFull = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public static Long toTs(String dtStr, boolean isFull) {
        
        LocalDateTime localDateTime = null;
        if (!isFull) {
            dtStr = dtStr + " 00:00:00"; //年月日时分秒
        }
        //     yyyy-MM-dd HH:mm:ss
        localDateTime = LocalDateTime.parse(dtStr, dtfFull);///yyyy-MM-dd HH:mm:ss

        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    public static Long toTs(String dtStr) {
        return toTs(dtStr, false);
    }

    public static String toDate(Long ts) {
        Date dt = new Date(ts);
        LocalDateTime localDateTime = LocalDateTime.ofInstant(dt.toInstant(), ZoneId.systemDefault());
        return dtf.format(localDateTime);
    }

    public static String toYmdHms(Long ts) {
        Date dt = new Date(ts);
//        ZoneId: 时区ID，用来确定Instant和LocalDateTime互相转换的规则
//        Instant: 用来表示时间线上的一个点（瞬时）
        LocalDateTime localDateTime = LocalDateTime.ofInstant(dt.toInstant(), ZoneId.systemDefault());
        return dtfFull.format(localDateTime);
    }


}